/**
 * 报价结算表格
 * Created by douxc on 2015/4/21.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('QuoteSettleCtrl', ['$scope', 'MHService','$location',
      function ($scope, MHService,$location) {
        //读出数据
        var costs = MHService.cacheData(false, {key: 'materialCosts'});
        if (!costs) {
          MHService.Toast('请添加材料');
          history.go(-2);
          return;
        }
        //计算材料总价
        var materialTotal = 0,//材料合计
          total = 0;//合计
        for (var m in costs.material) {
          materialTotal += costs.material[m].costs;
        }
        total = ((materialTotal + costs.siteFund +  costs.other)/(1-0.01*(costs.tax+costs.marketing+costs.management))).toFixed(2);
//        console.log(total);
        var _result = [];
        _result.push({name: '现场经费', percent: (costs.siteFund / total) * 100, costs: costs.siteFund.toFixed(2)});
        _result.push({name: '税金', percent: costs.tax, costs: ((costs.tax/100) * total).toFixed(2)});
        _result.push({name: '营销费', percent: costs.marketing, costs: ((costs.marketing/100) * total).toFixed(2)});
        _result.push({name: '管理费', percent: costs.management, costs: ((costs.management/100) * total).toFixed(2)});
        _result.push({name: '其他', percent: (costs.other / total) * 100, costs: costs.other.toFixed(2)});
        for (var m in costs.material) {
          var obj = costs.material[m];
          var _obj = {
            name: obj.name,
            percent: (obj.costs / total) * 100,
            costs: obj.costs
          };
          _result.push(_obj);
        }
        $scope.tables = _result;
        $scope.total = total;
          //完成提交数据
          $scope.next = function(){
              if (!materialTotal) {
                  MHService.Toast('材料未添加');
                  history.go(-2);
                  return;
              }else{
                  var  value = {
                      results : $scope.tables,
                      total : $scope.total
                  };
                  MHService.globalData(true, {key: 'total',value:value});
                  //跳转到下一页
                  $location.path("quoteSubmit");
              }

          }
      }]);
  };
});
